package com.tool.app.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author sun
 * @Date 2024/8/20
 */
//@Configuration
public class RedissonClientConfig {
    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;
    @Value("${spring.redis.database}")
    private int database;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.username}")
    private String username;
    @Value("${spring.profiles.active}")
    private String active;

    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        String[] split = clusterNodes.split(",");
        config.useClusterServers()
                .addNodeAddress(Arrays.stream(split).map(s -> "redis://" + s).toArray(String[]::new))
                .setUsername(username)
                .setPassword(password)
                .setScanInterval(2000)
                .setConnectTimeout(5000) // 连接超时时间
                .setSlaveConnectionPoolSize(64); // 连接池大小;

        if("dev".equals(active)){
            config.useClusterServers()
                  .setCheckSlotsCoverage(false);
        }
        return Redisson.create(config);
    }
}
